#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <errno.h>

#include "common.h"

int mt_mutex_init(mt_mutex_t *mtx) {
	return pthread_mutex_init(mtx, NULL);
}

int mt_mutex_lock(mt_mutex_t *mtx) {
	int res;
	uint32_t vp = mt_get_vcpu();
	_mt_thread_t *th = mt_thread_self();
	
	res = pthread_mutex_trylock(mtx);
	
	while(1) {
		if(res == 0) 
			return 0;
		
		if(res == EINVAL)
			return res;
		
		{_MT_SWITCH_CONTEXT(th, vp);}
		
		res = pthread_mutex_trylock(mtx);
	}
	
}

int mt_mutex_unlock(mt_mutex_t *mtx) {
	return pthread_mutex_unlock(mtx);
}
